//
//  TranstionAnimator.swift
//  上海微博
//
//  Created by zz on 16/3/11.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

//动画器-提供转场的细节,继承NSObject,遵循UIViewControllerTransitioningDelegate
class TranstionAnimator: NSObject,UIViewControllerTransitioningDelegate {

    var isPrensent: Bool = true
     /// 返回提供真正实现Present动画对象
    func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        
        isPrensent = true
        return self
    }
    
    /// 返回提供真正实现 dismiss 动画的对象
    func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        
        isPrensent = false
        return self
    }
}
// MARK: - UIViewControllerAnimatedTransitioning - 具体的动画实现功能逻辑
extension TranstionAnimator: UIViewControllerAnimatedTransitioning{

    func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
        return 3
    }
    /// 指定具体的动画逻辑 - 一旦实现了此方法，系统默认的转场失效，所有的动画需要程序猿来提供
    /// - parameter transitionContext: 转场上下文，提供转场所需的相关内容
    ///
    /// `转场`需要的内容，`从from`一个视图转换`到to`另外一个视图
    /// 1. 位置
    /// 2. 方式
    /// 3. 容器视图(存放`被展现视图`控制器的`视图`)－动画代码实现的`舞台`

    func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
        
        isPrensent ? presentTransition(transitionContext) : dismissTransition(transitionContext)
    }
 
//from MainViewController to TranstionViewController
    //present
    func presentTransition(transitionContext: UIViewControllerContextTransitioning) {
    
        //1.目标视图
        let toView = transitionContext.viewForKey(UITransitionContextToViewKey)!
        let toViewController = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey)
        
        print(toView)
        print(toViewController)
        //2.容器视图
        let containerView = transitionContext.containerView()
        //3.将目标视图添加到容器视图
        containerView?.addSubview(toView)
        
        toView.alpha = 0;
        //4.转场方式
        UIView .animateWithDuration(3, animations: {
            toView.alpha = 1
            
            }) { (_) in
                
                //5.完成转场动画,(缺失则导致无法UI交互）
                transitionContext.completeTransition(true)
        }
    
    }
    //dismiss
    func dismissTransition(transitionContext: UIViewControllerContextTransitioning) {
        
        let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey)
        let fromViewController = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey)
        
        
        UIView.animateWithDuration(3, animations: { 
            fromView?.alpha = 0
            }) { (_) in
                
                fromView?.removeFromSuperview()
                print(fromView)
                print(fromViewController)
                transitionContext.completeTransition(true)
        }
        
    }
    
}
